<?php
namespace Fox\Core;
use Fox;

/**
 * 分页导航类
 * 
 * @author keluo<pycorvn@yeah.net>
 * @since 2017-10-27 16:21:31
 */
class Page {
    
    public $offset; // 当前偏移量
    public $size; // 每页显示记录数
    
    private static $_instance;
    
    private function __construct($offset, $size) {
        $this->offset = $offset;
        $this->size = $size;
    }
    
    public function __clone() {}
    
    public static function Instance($size = 10) {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self(Fox::Request()->Get('offset'), $size);
        }
        return self::$_instance;
    }
    
    /**
     * 获取分页导航串形如：<< 2 3 4 5 ６ 7 8 9 10 11 >>
     * 可自定义每一项页码html格式，每一项必须包含{url}、{page}、{active}占位符，
     * {url}表示当前项url，{page}表示当前项页码，{active}表示当前项为激活项
     * SCS::page()->create('<li class="{active}"><a href="{url}">{page}</a></li>');
     * @param integer $total 总记录数
     * @param boolean $showTotal 是否显示总数
     * @param string $template 分页html模板
     * @return string
     */                                    
    public function Render($total, $showTotal = false, $column = 5, $template = '') {
        if(!strlen($template)) $template = '<li class="{active}"><a href="{url}">{page}</a></li>';
        $url = $this->Rebuild();
        $page = ceil($this->offset/$this->size)+1;
        $totalPage = ceil($total/$this->size);
        $numToShow = min($totalPage - 1, $column - 1);
        $firstIdx = max(1, $page - floor($numToShow/2));
        if($firstIdx + $numToShow > $totalPage) {
            $firstIdx = max(1, $totalPage - $numToShow);
        }
        if($totalPage <= 1) return '';
        $links = preg_replace(['/{url}/i', '/{page}/i', '/{active}/i'], 
            [$url.'&offset=0', '&lt;&lt;', (1 == $page) ? 'active' : ''], $template);
        for($i = $firstIdx; $i <= $numToShow + $firstIdx; $i++) {
            $links .= preg_replace(['/{url}/i', '/{page}/i', '/{active}/i'], 
                [$url.'&offset='.($i-1)*$this->size, $i, ($i == $page) ? 'active' : ''], $template);
        }
        $links .= preg_replace(['/{url}/i', '/{page}/i', '/{active}/i'], 
            [$url.'&offset='.($totalPage-1)*$this->size, '&gt;&gt;', ($totalPage == $page) ? 'active' : ''], $template);
        if($showTotal) $links .= '<li class="disabled"><a style="margin-left:1px;"href="javascript:;">'.$total.'</a></li>';
        echo $links;
    }
 
    protected function Rebuild($extends = []) {
        if($extends && is_array($extends)) {
            $params = array_merge(Fox::Request()->Get(), $extends);
        } else {
            $params = Fox::Request()->Get();
        }
        $url = Fox::Url()->Create(Fox::Request()->route).'?';
        if(isset($params['offset'])) unset($params['offset']);
        if(isset($params['_'])) unset($params['_']);
        foreach($params as $param=>$value) {
            $url .= $param.'='.urlencode($value).'&';
        }
        return rtrim($url, '&');
    }
}